/****************************************************************************
**
** CharacterListGump.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//---------------------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------------------
TCharacterListGump::TCharacterListGump()
{
	UO->m_Font->GenerateA(2, m_TextureCharacter, "Character Selection", 0x0386);
}
//---------------------------------------------------------------------------
TCharacterListGump::~TCharacterListGump()
{
	m_TextureCharacter.Clear();
}
//---------------------------------------------------------------------------
void TCharacterListGump::Init()
{
	char buf[128] = {0};
	sprintf(buf, "Ultima Online - %s (%s)", UO->GetAccount().c_str(), UO->GetServerName().c_str());

	SetWindowTextA(g_hWnd, buf);

	UO->m_CharacterList->SetSelected(0);
}
//---------------------------------------------------------------------------
int TCharacterListGump::Render(bool mode)
{
	DWORD ticks = GetTickCount();

	if (g_LastRenderTime > ticks)
	{
		if (mode || !g_SelectGumpObjects) return 0;
	}

	if (mode)
	{
		g_LastRenderTime = ticks + g_FrameDelay;

		UO->m_GL.BeginDraw();
		
		UO->DrawGump(0x0588, 0, 0, 0, 640, 480); //Main Gump background
		UO->DrawGump(0x157C, 0, 0, 0); //Main Gump
		UO->DrawGump(0x15A0, 0, 0, 4); //Main Gump Notes
		
		if (g_LastObjectLeftMouseDown == 1) UO->DrawGump(0x158B, 0, 555, 4); //X gump (lighted)
		else UO->DrawGump(0x1589 + (int)(g_LastSelectedObject == 1), 0, 555, 4); //X gump
		
		UO->DrawResizepicGump(0xA28, 160, 70, 408, 343); //Character list field

		m_TextureCharacter.Draw(267, 106);
		
		for (int i = 0; i < UO->m_CharacterList->GetCount(); i++)
		{
			UO->DrawResizepicGump(0xBB8, 224, 150 + (i * 40), 280, 30); //Character name field

			WORD FColor = 0x034F;
			if (UO->m_CharacterList->GetSelected() == i) FColor = 0x0021;
			else if (g_LastSelectedObject == (6 + i)) FColor = 0x03E3;

			UO->m_Font->DrawA(5, UO->m_CharacterList->GetName(i).c_str(), FColor, 229, 150 + (i * 40), 270.0f, TS_CENTER); //Character name
		}
		
		if (g_LastObjectLeftMouseDown == 4) UO->DrawGump(0x159F, 0, 224, 350); //New button pressed
		else UO->DrawGump(0x159D + (int)(g_LastSelectedObject == 4), 0, 224, 350); //New button
		
		if (g_LastObjectLeftMouseDown == 5) UO->DrawGump(0x159C, 0, 442, 350); //Delete button pressed
		else UO->DrawGump(0x159A + (int)(g_LastSelectedObject == 5), 0, 442, 350); //Delete button

		if (g_LastObjectLeftMouseDown == 2) UO->DrawGump(0x15A3, 0, 586, 445); //< gump pressed
		else UO->DrawGump(0x15A1 + (int)(g_LastSelectedObject == 2), 0, 586, 445); //< gump

		if (g_LastObjectLeftMouseDown == 3) UO->DrawGump(0x15A6, 0, 610, 445); //> gump pressed
		else UO->DrawGump(0x15A4 + (int)(g_LastSelectedObject == 3), 0, 610, 445); //> gump

		UO->DrawCursor(0x2073); //Main Gump mouse cursor

		UO->m_GL.EndDraw();
	}
	else
	{
		g_LastSelectedObject = 0;
		g_LastObjectType = SOT_NO_OBJECT;
		if (UO->GumpPixelsInXY(0x1589, 555, 4))
		{
			g_LastSelectedObject = 1; //X gump
		}

		for (int i=0; i<5; i++)
		{
			if (UO->ResizepicPixelsInXY(0xBB8, 224, 150 + (i * 40), 280, 30))
			{
				g_LastSelectedObject = 6 + i; //Character name field
			}
		}

		if (UO->GumpPixelsInXY(0x15A1, 586, 445))
		{
			g_LastSelectedObject = 2; //< gump
		}
		if (UO->GumpPixelsInXY(0x15A4, 610, 445))
		{
			g_LastSelectedObject = 3; //> gump
		}
		
		if (UO->GumpPixelsInXY(0x159D, 224, 350))
		{
			g_LastSelectedObject = 4; //New button
		}
		
		if (UO->GumpPixelsInXY(0x159A, 442, 350))
		{
			g_LastSelectedObject = 5; //Delete button
		}
		
		return g_LastSelectedObject;
	}
	return 0;
}
//---------------------------------------------------------------------------
void TCharacterListGump::ParseLeftMouseDown()
{
	Render(false);
	g_DroppedLeftMouseX = g_MouseX;
	g_DroppedLeftMouseY = g_MouseY;
	g_LeftMouseDown = true;
	g_LastObjectLeftMouseDown = g_LastSelectedObject;

	for (int i = 0; i < 5; i++)
	{
		if (g_LastSelectedObject == (6 + i))
		{
			UO->m_CharacterList->SetSelected(i);
			break;
		}
	}
}
//---------------------------------------------------------------------------
void TCharacterListGump::ParseLeftMouseUp()
{
	Render(false);
	g_LeftMouseDown = false;
	g_DroppedLeftMouseX = 0;
	g_DroppedLeftMouseY = 0;
	if (g_LastSelectedObject == 0 || g_LastSelectedObject != g_LastObjectLeftMouseDown)
	{
		g_LastObjectLeftMouseDown = 0;
		return;
	}

	if (g_LastObjectLeftMouseDown == 1) //x button
	{
		PostQuitMessage(0);
		if (UO != NULL) delete UO;
		UO = NULL;
		return;
	}
	else if (g_LastObjectLeftMouseDown == 2) //< button
	{
		UO->Connect();
		//UO->InitGump(GS_MAIN_CONNECT);
		return;
	}
	else if (g_LastObjectLeftMouseDown == 3) //> button
	{
		UO->CharacterSelection(UO->m_CharacterList->GetSelected());
		//UO->InitGump(GS_GAME_CONNECT);
		if (UO->m_CharacterList->GetName(UO->m_CharacterList->GetSelected()) == string("")) UO->SetGameConnectionError(2);
		return;
	}
	else if (g_LastObjectLeftMouseDown == 4) //New button
	{
		UO->InitGump(GS_CREATE_SELECT);
		return;
	}
	else if (g_LastObjectLeftMouseDown == 5) //Delete button
	{
		int Index = UO->m_CharacterList->GetSelected();
		if (UO->m_CharacterList->GetName(Index + 1) == string(""))
		{
			UO->m_CharacterList->SetName(Index, "");
		}
		else
		{
			for (int i = Index; i < UO->m_CharacterList->GetCount(); i++)
			{
				if (UO->m_CharacterList->GetName(i + 1) != string(""))
				{
					UO->m_CharacterList->SetName(i, UO->m_CharacterList->GetName(i + 1));
					UO->m_CharacterList->SetName(i + 1, "");
				}
				else break;
			}
		}
		g_LastObjectLeftMouseDown = 0;
		return;
	}

	g_LastObjectLeftMouseDown = 0;
}
//---------------------------------------------------------------------------
void TCharacterListGump::ParseRightMouseDown()
{
}
//---------------------------------------------------------------------------
void TCharacterListGump::ParseRightMouseUp()
{
}
//---------------------------------------------------------------------------
bool TCharacterListGump::ParseLeftMouseDoubleClick()
{
	Render(false);
	if (g_LastSelectedObject == 0 ) return false;

	for (int i = 0; i < UO->m_CharacterList->GetCount(); i++)
	{
		if (g_LastSelectedObject == (6 + i))
		{
			if (UO->m_CharacterList->GetName(i) == string("")) UO->InitGump(GS_CREATE_SELECT);
			else UO->CharacterSelection(UO->m_CharacterList->GetSelected()); //UO->InitGump(GS_GAME_CONNECT);
			return true;
		}
	}

	return false;
}
//---------------------------------------------------------------------------
bool TCharacterListGump::ParseRightMouseDoubleClick()
{
	return false;
}
//---------------------------------------------------------------------------
void TCharacterListGump::ParseMouseWheel(MOUSE_WHEEL_STATE state)
{
}
//---------------------------------------------------------------------------
void TCharacterListGump::ParseCharPress(WPARAM wparam, LPARAM lparam)
{
}
//---------------------------------------------------------------------------
void TCharacterListGump::ParseKeyPress(WPARAM wparam, LPARAM lparam)
{
}
//---------------------------------------------------------------------------